package com.derek.omshopping.ui.classify;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;

import com.alibaba.fastjson.JSONObject;
import com.derek.omshopping.R;
import com.derek.omshopping.bean.classify.ClassifyDetailBean;
import com.derek.omshopping.config.UrlConfig;
import com.derek.omshopping.http.RetrofitService;

import org.json.JSONArray;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

import static android.media.CamcorderProfile.get;

/**
 * Created by My on 2016/10/24.
 */

public class ClassifyDetailActivity extends AppCompatActivity {
    private GridView gv;
    private List<ClassifyDetailBean.PdtsAndActpicsBean.SetListBean.ContentListBean> list=new ArrayList<>();
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classifydetail);
        gv= (GridView) findViewById(R.id.gv_classifydetail);
        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String connect_tgt_addr=list.get(position).getConnect_tgt_addr();
                Intent intent=new Intent(ClassifyDetailActivity.this,PayActivity.class);
                intent.putExtra("imgId",connect_tgt_addr);
                startActivity(intent);
            }
        });
        init();
    }
    public void init(){
        Retrofit retrofit=new Retrofit.Builder().baseUrl(UrlConfig.CLASSIFY_DETAIL_BASEURL).build();
        Call<ResponseBody> call=retrofit.create(RetrofitService.class).getClassifyDetail();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String json=response.body().string();
                    List<ClassifyDetailBean> list_ClassifyDetailBean= com.alibaba.fastjson.JSONArray.parseArray(json,ClassifyDetailBean.class);
                    list=list_ClassifyDetailBean.get(0).getPdtsAndActpics().get(0).getSetList().get(0).getContentList();
                    ClassifyDetailAdapter adapter=new ClassifyDetailAdapter(getApplicationContext(),list);
                    gv.setAdapter(adapter);

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });

    }
}
